home *** CD-ROM | disk | FTP | other *** search
- import java.applet.*;
- import java.awt.*;
- import java.lang.*;
-
- public class convert extends Applet {
-
- TextField entry;
- TextArea result;
- final static String CONVERT = "Convert";
-
- public void init() {
- entry = new TextField(10) ;
- add(entry);
- add(new Button(CONVERT) );
- result = new TextArea() ;
- add(result);
- }
-
- public boolean action(Event evt, Object arg) {
- int val;
-
- if (arg.equals(CONVERT)) {
- String num = entry.getText();
-
- /*First, we try to conver the text to a
- value in base 10.*/
- try {
- val = Integer.parseInt(num);
- result.setText("In base 10, twice your number ");
- result.appendText("is: "+ 2*val+".\n");
- }
- catch(Exception e) {
- result.setText("Your number is not valid in ");
- result.appendText("base 10.\n");
- }
-
- /* Next, we try to convert the text to
- a binary number (base 2). */
- try {
- val = Integer.parseInt(num,2);
- result.appendText("In binary, twice your number ");
- result.appendText("is: "+ 2*val + ".\n");
- }
- catch (Exception e) {
- result.appendText("Your number is not a valid ");
- result.appendText("binary number.\n");
- }
- return true; // returns true only if the "Convert"
- // button was pressed (meaning that we
- // dealt with the event
- }
- return false; // returns false otherwise
- }
- }
-
-